package com.example.practice2023.service;

import com.example.practice2023.mapper.CarouselMapper;
import com.example.practice2023.pojo.Carousel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: CarouselService
 * Package: com.example.practice2023.service
 * Description:
 *
 * @Author hyx
 * @Create 2023/7/3 20:30
 * @Version 1.0
 */
@Service
public class CarouselService {
    @Resource
    private CarouselMapper carouselMapper;
//    @Resource
//     private ShopMapper shopMapper;
    public Map<String, Object> selectByPage(int page, int limit, Carousel carousel) {
        int index = (page - 1) * limit;   // 通用性的分页起始下标计算
        List<Carousel> carouselList = carouselMapper.selectByPage(index, limit, carousel);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", carouselMapper.selectAllCount(carousel));   // 通过数据库全查功能，把所有的记录个数统计出来
        map.put("data", carouselList);
        return map;
    }
    public Map<String, Object> select() {
        List<Carousel> carouselsList = carouselMapper.select();
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", carouselsList);
        return map;
    }
    public Map<String, Object> insert(Carousel carousel) {
        Map<String, Object> map = new HashMap<>();
        carousel.setUpdateTime(new Timestamp(System.currentTimeMillis()));  // util Date  sql Date  Time  Timestamp
        carouselMapper.insert(carousel);
        map.put("code", 200);
        map.put("msg", "新增成功");
        map.put("data", carousel);
        return map;
    }
    public Map<String, Object> updateById(Carousel carousel) {
        Map<String, Object> map = new HashMap<>();
        carousel.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        carouselMapper.updateById(carousel);
        map.put("code", 200);
        map.put("msg", "更新成功");
        map.put("data", carousel);
        return map;
    }
    public Map<String, Object> updateByStatus(Carousel carousel) {
        Map<String, Object> map = new HashMap<>();
        carouselMapper.updateByStatus(carousel);
        map.put("code", 200);
        map.put("msg", "更新成功");
        map.put("data", carousel);
        return map;
    }
    public Map<String, Object> deleteById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        carouselMapper.deleteById(id);
        map.put("code", 200);
        map.put("msg", "删除成功");
        map.put("id", id);
        return map;
    }
}
